package com.firebird.app.kotlin.stock.trend.activity

import android.app.Activity
import android.content.DialogInterface
import android.os.Bundle
import android.util.Base64
import android.util.TypedValue
import android.view.*
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.widget.Button
import android.widget.TextView
import com.android.volley.Request
import com.android.volley.Response
import com.firebird.app.kotlin.stock.trend.StockApp
import com.firebird.app.kotlin.stock.trend.bean.News
import com.firebird.app.kotlin.stock.trend.view.SimpleCommonTitleView
import com.firebird.app.kotlin.stock.trend.R
import com.firebird.app.kotlin.stock.trend.net.ObjectRequest
import com.firebird.app.kotlin.stock.trend.view.CustomDialog
import com.firebird.app.kotlin.stock.trend.view.FontDialog
import com.google.gson.reflect.TypeToken
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*


/**
 * 显示新闻详情的窗口
 * @author ljk
 * @since  2019年4月6日
 */
class NewsDetailActivity : Activity() {
    //通用窗口title
    private var simpleTitle: SimpleCommonTitleView? = null
    private var name_tv: TextView? = null
    private var time_tv: TextView? = null
    private var from_tv: TextView? = null
    private var fenxiang_tv: TextView? = null
    private var review_but: Button? = null
    private var webView: WebView? = null
    private var fontSize = 1
    override fun onCreate(savedInstanceState: Bundle) {
        super.onCreate(savedInstanceState)
        this.setContentView(R.layout.activity_news_detail)
        //初始化titlebar
        simpleTitle = findViewById<View>(R.id.simple_title) as SimpleCommonTitleView
        simpleTitle!!.setOnLeftClickListener(MyOnClickListener())
        simpleTitle!!.setOnRightClickListener(MyOnClickListener())
        //初始化分享按钮
        fenxiang_tv = findViewById<View>(R.id.fenxiang) as TextView
        fenxiang_tv!!.setOnClickListener(MyOnClickListener())
        name_tv = findViewById<View>(R.id.news_name) as TextView
        time_tv = findViewById<View>(R.id.news_time) as TextView
        from_tv = findViewById<View>(R.id.news_from) as TextView
        review_but = findViewById<View>(R.id.news_review) as Button
        webView = findViewById<View>(R.id.news_content) as WebView
        initData()
    }

    /**
     * 加载数据
     */
    fun initData() { //TODO初始化新闻数据
        val url = this.resources.getString(R.string.url_news_detail)
        val successListener: Response.Listener<List<News>> = Response.Listener<List<News>> { resultList ->
            //System.out.println("============== "+resultList.get(0).getTitle());
            if (resultList != null && resultList.size > 0) {
                val news = resultList[0]
                //String html = Base64.encodeToString(news.getContent().getBytes(), Base64.CRLF);
                val html = String(Base64.decode(news.content, Base64.CRLF))
                webView!!.loadDataWithBaseURL(null, html, "text/html", "utf-8", null)
                webView!!.settings.javaScriptEnabled = true
                webView!!.webChromeClient = WebChromeClient()
            }
        }
        val errorListener = Response.ErrorListener { println("============== 失败") }
        val newsRequest: Request<List<News>> = ObjectRequest(Request.Method.GET, object : TypeToken<List<News?>?>() {}.getType(), url, successListener, errorListener)
        StockApp.requestQueue!!.add(newsRequest)
        val intent = this.intent
        val news = intent.getSerializableExtra("news") as News
        name_tv!!.text = news.title
        val df: DateFormat = SimpleDateFormat("MM-dd hh:mm", Locale.CHINA)
        time_tv!!.text = df.format(news.createTime)
        from_tv!!.text = "来源：" + news.from
    }

    /**
     * 内部类 处理按钮点击事件
     * @author zemg
     * @since  2016年4月13日
     */
    private inner class MyOnClickListener : View.OnClickListener {
        override fun onClick(v: View) {
            when (v.id) {
                R.id.title_right_but -> fontChange()
                R.id.title_left_but -> finish()
                R.id.fenxiang -> fenxiang()
            }
        }

        //处理字体切换按钮点击事件
        private fun fontChange() {
            val builder: FontDialog.Builder = FontDialog.Builder(this@NewsDetailActivity)
            builder.setNegativeButton("确定", DialogInterface.OnClickListener { dialog, a ->
                when (fontSize) {
                    0 -> {
                        name_tv!!.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24f)
                        time_tv!!.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
                        from_tv!!.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
                        review_but!!.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
                        webView!!.settings.textZoom = 120
                    }
                    1 -> {
                        name_tv!!.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f)
                        time_tv!!.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f)
                        from_tv!!.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f)
                        review_but!!.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f)
                        webView!!.settings.textZoom = 100
                    }
                    2 -> {
                        name_tv!!.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
                        time_tv!!.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10f)
                        from_tv!!.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10f)
                        review_but!!.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10f)
                        webView!!.settings.textZoom = 80
                    }
                }
                dialog.dismiss()
            })
            builder.setPositiveButton("取消", DialogInterface.OnClickListener { dialog, arg1 -> dialog.dismiss() })
            builder.setSingleChoiceItems(1, fontSize, DialogInterface.OnClickListener { dialog, item ->
                fontSize = item
                //Toast.makeText(NewsDetailActivity.this, "单选按钮："+item, Toast.LENGTH_SHORT).show();
            })
            builder.setTitle("正文字体")
            val dialog: FontDialog = builder.create()
            dialog.show()
        }

        //处理分享点击事件
        private fun fenxiang() {
            val builder: CustomDialog.Builder = CustomDialog.Builder(this@NewsDetailActivity)
            builder.setPositiveButton("取消", DialogInterface.OnClickListener { dialog, arg1 -> dialog.dismiss() })
            val dialog: CustomDialog = builder.create()
            //获得当前窗体
            val window: Window = dialog.getWindow()
            window.setGravity(Gravity.BOTTOM)
            val wm = windowManager
            val display = wm.defaultDisplay
            val lp: WindowManager.LayoutParams = dialog.getWindow().getAttributes()
            lp.width = display.width
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT
            dialog.getWindow().setAttributes(lp)
            dialog.show()
        }
    }
}